<?php
class TengxunController extends Controller {

	private function tengxunini(){
		session_start();
		header('Content-Type: text/html; charset=utf-8');
 		set_include_path(dirname(dirname(__FILE__)) . '/extensions/tengxun/');
 		require_once 'OpenSDK/Tencent/Weibo.php';
 		require_once 'appkey.php';

		//获取token
		$tengxun_token_model = $this->model('tengxun_token');
		$tokendata = $tengxun_token_model->from('tengxun_token')->where('userid=?', $_SESSION['userid'])->select();	

		if(empty($tokendata)){
			$url = $this->create_url('authorize/tengxun');
			$this->show_message('用户授权腾讯微博，即将转入授权页面',$url,1 );
		}

		//腾讯数据初始化
		$_SESSION['tencent_oauth_token'] = $tokendata[0]['oauth_token'];
		$_SESSION['tencent_oauth_token_secret'] = $tokendata[0]['oauth_token_secret'];
		$_SESSION['tencent_access_token'] = $tokendata[0]['access_token'];

		// OpenSDK_Tencent_Weibo::init($appkey, $appsecret);
		// OpenSDK_Tencent_Weibo::getAccessToken($_GET['oauth_verifier']);

		$c = new OpenSDK_Tencent_Weibo(); 
		$c->init($appkey, $appsecret);
		$c->getAccessToken($_GET['oauth_verifier']);
			
		return $c;
	}

	//首页
	public function indexAction() {
		$c = $this->tengxunini();

		$uinfo = $c->call('user/info');
		$homemessage = $c->call('statuses/home_timeline');

		$this->assign('data',$uinfo);
		$this->assign('homemessage',$homemessage);
		$this->display();
	}

	//ajax动态加载 
	public function ajaxAction() {
		$c = $this->tengxunini();

		$uinfo = $c->call('user/info');
		$homemessage = $c->call('statuses/home_timeline');

		$this->assign('data',$uinfo);
		$this->assign('homemessage',$homemessage);
		$this->set_layout("main_no");
		$this->display();
	}

	public function detailsAction(){
		$mid = $this->get_params('mid');
		$c = $this->tengxunini();
		$homemessage = $c->call('t/show' , array('id' => $mid , 'format' =>'json'));

		$this->assign('homemessage',$homemessage);
		$this->set_layout("main_no");
		$this->display();
	}

	public function pinglunAction(){
		$mid = $this->get_params('mid');
		$mid = 88775052415527;
		// $content = $this->get_params('pinglun');
		$content = '我要评论 ';
		$ip = '61.146.57.222';

		$c = $this->tengxunini();
		$pinglun = $c->call('t/comment' , array('reid' => $mid , 'format' =>'json' , 'content' => "今天很开心" , "clientip" => $ip ));
		
	}

	//授权
	public function authorizeAction() {
		session_start();
 		 set_include_path(dirname(dirname(__FILE__)) . '/extensions/tengxun/');
 		 require_once 'OpenSDK/Tencent/Weibo.php';
 		 require_once 'appkey.php';
		header('Content-Type: text/html; charset=utf-8');
		OpenSDK_Tencent_Weibo::init($appkey, $appsecret);
		header('Content-Type: text/html; charset=utf-8');
		echo $this->get_server_name();
		echo $callback = $this->get_server_name() . $this->create_url('tengxun/callback');
		$request_token = OpenSDK_Tencent_Weibo::getRequestToken($callback);
		$url = OpenSDK_Tencent_Weibo::getAuthorizeURL($request_token);
		header('Location: ' . $url);
	}

	//回调函数
	public function callbackAction() {
		session_start();
		header('Content-Type: text/html; charset=utf-8');
 		 set_include_path(dirname(dirname(__FILE__)) . '/extensions/tengxun/');
 		 require_once 'OpenSDK/Tencent/Weibo.php';
 		 require_once 'appkey.php';
		header('Content-Type: text/html; charset=utf-8');
		OpenSDK_Tencent_Weibo::init($appkey, $appsecret);

		//从Callback返回时
		if(OpenSDK_Tencent_Weibo::getAccessToken($_GET['oauth_verifier']))
		{
			//token 入库
			$tengxun_token_model = $this->model('tengxun_token');
			$tokendata = array();
			$tokendata['userid'] = $_SESSION['userid'] ;
			$tokendata['oauth_token'] = $_SESSION['tencent_oauth_token'] ;
			$tokendata['oauth_token_secret'] = $_SESSION['tencent_oauth_token_secret'] ;
			$tokendata['access_token'] = $_SESSION['tencent_access_token'] ;
			$tokendata['openid'] = $_GET['openid'] ;

			$data = $tengxun_token_model->from('tengxun_token')->where('access_token=?', $tokendata['access_token'])->select();

			if(!empty($data)){
				$url = $this->create_url('tengxun/index');
				$this->show_message('授权成功，即将转入腾讯微博',$url,1 );
			}else{
				if($tengxun_token_model->insert($tokendata)){
					$url = $this->create_url('tengxun/index');
					$this->show_message('授权成功，即将转入腾讯微博',$url,1 );
				}
			}

		}
	}

	public function testAction(){
		$mid = $this->get_params('mid');
		$c = $this->tengxunini();
		$homemessage = $c->call('t/show' , array('id' => $mid , 'format' =>'json')); 
		$coment = $c->call('t/re_list' , array('rootid' => $mid , 'type' =>'1'));
		// print_r($homemessage);
		print_r($coment);
		// $this->assign('homemessage',$homemessage);

		// $this->set_layout("main_no");
		// $this->display();
	}
}